#include "linkedlist.h"

LinkedList::LinkedList()
{
    head = new node( NULL, NULL, 0, 0);
    tail = head;
    head->next = head;
    head->prev = head;
    n = 1;
    step = 2.0f;
}

int LinkedList::up(BOOL add){

    if( add )
    {

        node* t = new node( head, tail, head->x, head->y + step);
        head->prev = t;
        tail->next = t;
        head    = t;
        n++;

    }else{

        tail->x = head->x;
        tail->y = head->y + step;
        head = tail;
        tail = tail->prev;

    }

    return 1;
}

int LinkedList::down(BOOL add){

    if( add )
    {

        node* t = new node( head, tail, head->x, head->y - step);
        head->prev = t;
        tail->next = t;
        head    = t;
        n++;

    }else{

        tail->x = head->x;
        tail->y = head->y - step;
        head = tail;
        tail = tail->prev;

    }

    return 1;
}


int LinkedList::left(BOOL add){

    if( add )
    {

        node* t = new node( head, tail, head->x - step, head->y);
        head->prev = t;
        tail->next = t;
        head    = t;
        n++;

    }else{

        tail->x = head->x - step;
        tail->y = head->y;
        head = tail;
        tail = tail->prev;

    }

    return 1;
}

int LinkedList::right(BOOL add){

    if( add )
    {

        node* t = new node( head, tail, head->x + step, head->y);
        head->prev = t;
        tail->next = t;
        head    = t;
        n++;

    }else{

        tail->x = head->x + step;
        tail->y = head->y;
        head = tail;
        tail = tail->prev;

    }

    return 1;
}


void LinkedList::drawList(){
    int k = 0;
    for(node* t = head; k < n; t = t->next)
    {
        //cout<<"posx = "<<t->x<<"\n";
        glVertex3f(-1.0f + t->x, 1.0f + t->y, 0.0f);
        glVertex3f( 1.0f + t->x, 1.0f + t->y, 0.0f);
        glVertex3f( 1.0f + t->x,-1.0f + t->y, 0.0f);
        glVertex3f(-1.0f + t->x,-1.0f + t->y, 0.0f);
        k++;
    }

}
